//
//  HMWelcomController.swift
//  weibo10
//
//  Created by mac on 15/10/21.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SDWebImage

class HMWelcomController: UIViewController {
    
    override func loadView() {
        view = imageView
        setupUI()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置头像数据
        iconView.sd_setImageWithURL(HMUserAcountViewModel.sharedUserAccount.iconUrl, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    // MARK -懒加载控件
    private lazy var imageView: UIImageView = UIImageView(imageName: "ad_background")
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(imageName: "avatar_default_big")
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        return imageView
    }()
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎归来", titleColor: UIColor.darkGrayColor(), fontSize: 16)
    
}
extension HMWelcomController{
    // MARK -设置动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        UIView.animateWithDuration(1.0,
            delay: 0,
            usingSpringWithDamping: 0.5,
            initialSpringVelocity: 10,
            options: [], animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.7, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("Ok")
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
                })
        }
    }
    // MARK -设置UI
    private func setupUI(){
        // 添加控件
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        welcomeLabel.alpha = 0
        // 设置布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
}

